package com.hlx.controller;

import com.hlx.config.FileConfig;
import com.hlx.entity.DirectoryTree;
import com.hlx.entity.FileAttributes;
import com.hlx.service.FileTreeService;
import com.hlx.util.AESUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("file")
public class FileTreeController {
    @Autowired
    private FileTreeService fileTreeService;
    @Autowired
    private FileConfig fileConfig;


    /**
     * 文件列表
     */
    @GetMapping("fileList")
    public List<FileAttributes> fileList(String directory) {
        try {
            directory = AESUtil.decrypt(directory, AESUtil.KEY);// 解密
            File file = new File(directory);
            return fileTreeService.fileList(file);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    /**
     * 获取目录树
     */
    @GetMapping("directoryTree")
    public Map<String, Object> getDirectoryTree() {
        try {
            File directory = new File(fileConfig.getUrl());
            DirectoryTree treeNode = fileTreeService.directoryTree(directory);
            Map<String, Object> response = new HashMap<>();
            response.put("treeData", treeNode);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap<>();
        }
    }


    /**
     * 文件下载
     */
    @GetMapping("download")
    public ResponseEntity<FileSystemResource> download(String path) {
        try {
            path = AESUtil.decrypt(path, AESUtil.KEY);
            File file = new File(path);
            return fileTreeService.download(file);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(null);
        }
    }

    /**
     * 递归删除
     */
    @DeleteMapping("delete")
    public boolean delete(String path) {
        try {
            path = AESUtil.decrypt(path, AESUtil.KEY);
            File file = new File(path);
            fileTreeService.delete(file);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 文件重命名
     */
    @PutMapping("update")
    public boolean update(String path, String newFile) {
        try {
            path = AESUtil.decrypt(path, AESUtil.KEY);
            File file = new File(path);
            fileTreeService.update(file, newFile);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 创建目录
     */
    @PostMapping("createDir")
    public boolean createDir(String path, String dirName) {
        try {
            path = AESUtil.decrypt(path, AESUtil.KEY);
            File file = new File(path, dirName);
            return file.mkdirs();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}